const OpenAI = require("openai");
const { AI_KEY } = require('../config/config.development');

class AiController {
    async DeepSeek(ctx) {
        const openai = new OpenAI({
            baseURL: 'https://api.deepseek.com',
            apiKey: AI_KEY
        });

        // 从请求体中获取用户输入
        const userContent = ctx.request.body.content;
        
        // 验证用户输入
        if (!userContent) {
            ctx.body = {
                code: 400,
                message: '请输入有效内容',
                data: ""
            }
            return;
        }

        try {
            const completion = await openai.chat.completions.create({
                messages: [
                    { role: "system", content: "你是一个乐于助人的助手。" },
                    { role: "user", content: userContent } // 添加用户消息
                ],
                model: "deepseek-chat",
            });

            ctx.body = {
                code: 200,
                message: '请求成功',
                data: {
                    reply: completion.choices[0].message.content
                }
            }
        } catch (error) {
            console.error('API调用错误:', error);
            ctx.body = {
                code: 500,
                message: '服务暂时不可用，请稍后再试',
                data: ""
            }
        }
    }
}

module.exports = new AiController()